package com.sky.common.docker.container;

import com.github.dockerjava.api.command.InspectContainerResponse.ContainerState;
import com.sky.common.enums.ContainerStatus;
import com.sky.common.utils.DateUtils;
import lombok.Data;

import java.text.ParseException;
import java.util.Date;

/**
 * 容器状态信息
 *
 */
@Data
public class ContainerStateInfo {
    /**
     * 容器状态
     */
    private ContainerStatus status;
    /**
     * 是否运行
     */
    private boolean running;

    /**
     * 是否静止
     */
    private boolean paused;
    /**
     * 是否重启
     */
    private boolean restarting;
    /**
     * 是否内存不足被杀死
     */
    private boolean oomKilled;
    /**
     * 是否死亡
     */
    private boolean dead;
    /**
     * 进程id
     */
    private Long pid;
    /**
     * 退出code
     */
    private Long exitCode;
    /**
     * 错误信息
     */
    private String error;
    /**
     * 开始时间
     */
    private Date startedAt;
    /**
     * 结束时间
     */
    private Date finishedAt;

    /**
     * 解析容器状态信息
     *
     * @param containerState 容器状态
     * @return 容器状态信息
     */
    public static ContainerStateInfo valueOf(ContainerState containerState) {
        ContainerStateInfo info = new ContainerStateInfo();
        ContainerStatus status = ContainerStatus.valueOf(containerState.getStatus());
        info.setStatus(status);
        info.setRunning(containerState.getRunning() != null ? containerState.getRunning() : false);
        info.setPaused(containerState.getPaused() != null ? containerState.getPaused() : false);
        info.setRestarting(containerState.getRestarting() != null ? containerState.getRestarting() : false);
        info.setOomKilled(containerState.getOOMKilled() != null ? containerState.getOOMKilled() : false);
        info.setDead(containerState.getDead() != null ? containerState.getDead() : false);
        info.setPid(containerState.getPidLong());
        info.setExitCode(containerState.getExitCodeLong());
        info.setError(containerState.getError());
        String startAt = containerState.getStartedAt();
        String finishedAt = containerState.getFinishedAt();
        Date startAtDate = null;
        Date finishedAtDate = null;
        if (startAt != null && !startAt.contains("0001-01-01")) {
            String startDateStr = startAt.substring(0, 19);
            if (startAt.toUpperCase().endsWith("Z")) {
                startDateStr += "UTC";
            }
            try {
                startAtDate = DateUtils.parse(startDateStr, "yyyy-MM-dd'T'HH:mm:ssZ");
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        if (finishedAt != null && !finishedAt.contains("0001-01-01")) {
            String finishedDateStr = finishedAt.substring(0, 19);
            if (finishedAt.toUpperCase().endsWith("Z")) {
                finishedDateStr += "UTC";
            }
            try {
                finishedAtDate = DateUtils.parse(finishedDateStr, "yyyy-MM-dd'T'HH:mm:ssZ");
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        info.setStartedAt(startAtDate);
        info.setFinishedAt(finishedAtDate);
        return info;
    }

}
